﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CE.Global;
using CE.Provider;

public partial class provider_ProposalForm_RenewalDownloadConfirmation : System.Web.UI.Page
{
    string PageName = "CXDPF";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session[CE.Global.General.UserId] == null || Session[CE.Global.General.ProviderCompanyId] == null)
        {
            Response.Redirect("~/Provider/ProviderSecureLogin.aspx");
        }
        CheckMainPageAccess();

        if (Request.QueryString["pid"] == null || Request.QueryString["type"] == null || Request.QueryString["dl"] == null || Request.QueryString["prid"] == null || Request.QueryString["ryr"] == null)
        {
            Response.Redirect("RenewalProposalDownLoad.aspx");
        }

        if (!Page.IsPostBack)
        {
            lblPID.Text = Request.QueryString["pid"].ToString();
            lblType.Text = Request.QueryString["type"].ToString();
            lblDLid.Text = Request.QueryString["dl"].ToString();
            lblPremiumId.Text = Request.QueryString["prid"].ToString();
            lblRenewalYear.Text = Request.QueryString["ryr"].ToString();

            if (lblType.Text == "TI")
            {
                lblInsType.Text = "Travel Insurance";
            }
            else if (lblType.Text == "CI")
            {
                lblInsType.Text = "Car Insurance";
            }
            else if (lblType.Text == "PA")
            {
                lblInsType.Text = "Personal Accident";
            }
            string convertno = GetReferenceNumber(Request.QueryString["pid"].ToString(), Request.QueryString["type"].ToString());
            lblFilename.Text = convertno.ToString();

        }
    }

    protected void btnConfirm_Click(object sender, ImageClickEventArgs e)
    {

        Int64 ProviderCompanyId = Convert.ToInt64(Session[CE.Global.General.ProviderCompanyId].ToString());
        Int64 RefId = Convert.ToInt64(lblPID.Text);
        Boolean isConfirm = Boolean.Parse(rblConfirmation.SelectedValue);
        Int64 UserId = Convert.ToInt64(Session[CE.Global.General.UserId].ToString());
        Int64 PremiumId = Convert.ToInt64(lblPremiumId.Text);
        Int32 RenewalYear = Convert.ToInt32(lblRenewalYear.Text);

        string InsType = lblType.Text.Trim();

        ProposalDownloadDB _proposaldb = new ProposalDownloadDB();

        Int64 DownloadId = Convert.ToInt64(lblDLid.Text);
        Int64 ResultId = 0;

        if (isConfirm)
        {
            if (InsType == "CI")
            {
                ResultId = _proposaldb.UpdateCarRenewalDownloadInfo(RefId, ProviderCompanyId, true, isConfirm, 1, DownloadId, UserId, PremiumId,RenewalYear);
            }
        }

        Response.Redirect("RenewalProposalDownLoad.aspx");

    }

    protected void btnBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("RenewalProposalDownLoad.aspx");
    }

    public string GetReferenceNumber(string Number, string InsType)
    {
        string convertno = General.GetRefNumber(Convert.ToInt64(Number.ToString()));
        string _RefNo = "";
        if (Session[CE.Global.General.ProviderCompanyId].ToString() == "23")
        {
            if (InsType == "PA")
            {
                _RefNo = "PAH" + convertno.ToString();
            }
            else if (InsType == "TI")
            {
                _RefNo = "TIH" + convertno.ToString();
            }
            else
            {
                _RefNo = "H" + convertno.ToString();
            }
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "30")
        {
            _RefNo = "E" + convertno.ToString();
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "29")
        {
            if (InsType == "PA")
            {
                _RefNo = "PAL" + convertno.ToString();
            }
            else if (InsType == "TI")
            {
                _RefNo = "TIL" + convertno.ToString();
            }
            else
            {
                _RefNo = "L" + convertno.ToString();
            }
        }
        else if (Session[CE.Global.General.ProviderCompanyId].ToString() == "24")
        {
            if (InsType == "TI")
            {
                _RefNo = "TIR" + convertno.ToString();
            }
            else
            {
                _RefNo = "R" + convertno.ToString();
            }
        }
        return _RefNo;
    }

    private void CheckMainPageAccess()
    {
        Int64 PageAccess = General.GetProviderMainPageAccess(Convert.ToInt64(Session[General.ProviderCompanyId].ToString()),
                                                        Convert.ToInt64(Session[General.UserId].ToString()), PageName);
        if (PageAccess == 0)
        {
            Response.Redirect("~/Provider/UserMessage.aspx?mt=UM&ec=2000");
        }
    }

}
